package com.zxj.camerademo.camera1;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.util.Log;
import android.view.Surface;
import android.widget.Toast;

import com.zxj.camerademo.utils.FileUtil;

import java.io.IOException;

public class MediaRecorderHelper {
    private int rotation;
    private Surface surface;
    private Activity mContext;
    private Camera mCamera;
    private boolean isRunning;
    private String filePath = FileUtil.createVideoFile().getAbsolutePath();
    private MediaRecorder mMediaRecorder;

    public MediaRecorderHelper(Activity context, Camera camera, int rotation, Surface surface){
        this.mContext = context;
        this.mCamera = camera;
        this.rotation = rotation;
        this.surface = surface;
    }

    public void startRecord(){
        mMediaRecorder = new MediaRecorder();
        mCamera.unlock(); //一定要调用
        mMediaRecorder.reset();
        mMediaRecorder.setCamera(mCamera);            //给Recorder设置Camera对象
        mMediaRecorder.setOrientationHint(rotation);  //改变保存后的视频文件播放时是否横屏(不加这句，视频文件播放的时候角度是反的)
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);      //设置从麦克风采集声音
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);   //设置从摄像头采集图像
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置视频的输出格式为MP4
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //设置音频的编码格式
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); // 设置视频的编码格式
        mMediaRecorder.setVideoSize(3840, 2160);// 设置视频大小
        mMediaRecorder.setVideoFrameRate(60);  // 设置帧率
        //mMediaRecorder.setMaxDuration(10000); //设置最大录像时间为10s
        mMediaRecorder.setPreviewDisplay(surface); //设置
        mMediaRecorder.setOutputFile(filePath); //设置输出文件
        try {
            mMediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaRecorder.start();
        isRunning = true;

        Log.d("tag", "开始录制视频");
        Log.d("tag", "视频保存路径："+filePath);
    }

    public void stopRecord(){
        if(mMediaRecorder != null) {
            //下面三个参数必须加，不加的话会奔溃，在mediarecorder.stop();
            //报错为：RuntimeException:stop failed
//            mMediaRecorder.setOnErrorListener(null);
//            mMediaRecorder.setOnInfoListener(null);
//            mMediaRecorder.setPreviewDisplay(null);
            mMediaRecorder.stop();
            mMediaRecorder.release();
            mMediaRecorder = null;
        }
        Log.d("tag", "停止录制视频");
        if(isRunning)
            Toast.makeText(mContext, isRunning+"视频保存路径："+filePath, Toast.LENGTH_LONG).show();
        isRunning = false;
    }

    private void release(){
        if(mMediaRecorder != null){
            mMediaRecorder.release();
            mMediaRecorder = null;
            isRunning = false;
        }
    }
}
